package com.jia.lidao.ui.message;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/11/20.
 */

public abstract class BaseFragment extends Fragment {


    protected  boolean isFirstIn = true;
    private Toast toast;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        try{
            view = inflater.inflate(getInflateLayout(),container ,false);
        }catch (Throwable throwable){
            System.gc();
            view = View.inflate(getActivity(),getInflateLayout(),container);
        }
        setUpView(view);
        return view;
    }

    /*加载页面的布局定义*/
    protected abstract int  getInflateLayout();

    protected abstract void setUpView(View view);

    public void showMsgToast(String msg) {
        if (null == toast) {
            toast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();

    }


    /**
     * 检查当前网络是否可用
     *
     * @param
     * @return
     */

    public boolean isNetworkAvailable()
    {
        Context context = getActivity().getApplicationContext();
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null)
        {
            return false;
        }
        else
        {
            // 获取NetworkInfo对象
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0)
            {
                for (int i = 0; i < networkInfo.length; i++)
                {
                    System.out.println(i + "===状态===" + networkInfo[i].getState());
                    System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    protected void showWifiSettingDialog() {
        AlertDialog.Builder builer = new AlertDialog.Builder(getContext());
        builer.setTitle("当前无网络");


        builer.setPositiveButton("设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // 跳转到系统的网络设置界面
         /*       Intent intent = null;
                // 先判断当前系统版本
                if(android.os.Build.VERSION.SDK_INT > 10){  // 3.0以上
                    intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                }else{
                    intent = new Intent();
                    intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
                }
                startActivity(intent);*/

                Intent intent =  new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
                startActivity(intent);

            }
        });

        AlertDialog dialog = builer.create();
        dialog.setCancelable(false);
        dialog.show();
    }



}
